<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane v-loading="loading" label="组织架构" name="first">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>负责人</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="add('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-divider />
            <!-- 树形图 -->
            <el-tree :data="list" :default-expand-all="true">
              <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
              <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
              <template #default="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  align="middle"
                  style="height: 40px; width: 100%"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <!-- 两个内容 -->
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <!-- 下拉菜单 element -->
                        <el-dropdown>
                          <span> 操作<i class="el-icon-arrow-down" /> </span>
                          <!-- 下拉菜单 -->
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="add(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="del(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="odd(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 用一个行列布局 -->
      </el-card>
      <!-- 弹框 -->
      <tan
        :id="id"
        ref="reference"
        :shuju="shuju"
        :is-all="isAll"
        :state.sync="state"
        @comm="getlist"
        @shuaxin="getlist"
      />
      <!--  -->
    </div>
  </div>
</template>
<script>
import { getorganization, del } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import { xiangqin } from '@/api/employess'
import tan from './com/tan.vue'
export default {
  name: 'Department',
  components: {
    tan: tan
  },
  data() {
    return {
      loading: false,
      // 依赖一份树形数据
      list: [],
      activeName: 'first',
      state: false,
      id: '',
      isAll: false,
      shuju: {}
    }
  },
  created() {
    this.getlist()
  },
  mounted() {},

  methods: {
    async getlist() {
      console.log(11)
      this.loading = true
      const res = await getorganization()
      this.list = tranListToTreeData(res.depts)
      this.loading = false
    },
    // 删除
    odd(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          console.log(id)
          await del(id)
          this.getlist()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加
    add(pid) {
      this.isAll = false
      this.state = true
      this.id = pid
    },
    // 编辑
    del(pid) {
      this.isAll = true
      this.state = true

      this.xiangqin(pid)
    },
    // 详情
    async xiangqin(id) {
      this.id = id
      console.log(id)
      const res = await xiangqin(id)
      console.log(res)
      this.shuju = res
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  padding: 20px;
  .el-tab-pane {
    padding: 20px 120px;
  }
}
</style>

